/* 
 * 抽奖活动页面.
 */

import { Component, OnInit } from '@angular/core';
import { Member } from '../domain/Member';

import { RestService } from '../services/RestService';
import { Winner } from "../domain/Winner";
import { Bingo } from "../domain/Bingo";
import { Prize } from "../domain/Prize";

import { SelectItem } from 'primeng/primeng';


@Component({
    selector: 'page-roulette',
    templateUrl: './roulette.component.html',
    styleUrls: ['./roulette.component.css']
})
export class RouletteComponent implements OnInit{


    rotationHeight : number = 150;

    rotationRepeat : number = 150;

    rotationOffset : number = 0;

    rotationSpeed : number = 5;


	rotation : number = 0;

	members : Member []; // 显示...

    memberItems : Member []; // 参与人员...

	winner : Winner;

	winners : Winner[];

    bingo : Bingo;

    prize : Prize;

    prizeItems: SelectItem[];

    ready : boolean = true;

    rolling : boolean = false;

	
    constructor(private restService: RestService) {
    	this.prize = new Prize();
        this.prizeItems = new Array();

        this.winners = new Array();
	}
	
	ngOnInit() {
		console.log("初始化抽奖人员...");

        this.init();
	}

    async init() {
        console.log("初始化奖项设置界面...");

        // 这里是同步顺序执行的
        // 不需要写成then的两个嵌套
        this.bingo = await this.restService.getCurrentBingo();

        let prizes_ = await this.restService.getPrizesByBingo(this.bingo.id);

        for (let i = 0; i < prizes_.length; i ++) {
            this.prizeItems[i] = {label:prizes_[i].name, value:prizes_[i]};
        }

        if (this.prizeItems.length > 0)
        	this.prize = this.prizeItems[0].value;

        this.members = await this.restService.getMembersByBingo(this.bingo.id);

        this.memberItems = new Array(this.members.length * 2 + 1);

        for (let j = 0; j < this.memberItems.length; j ++) {
            this.memberItems[j] = this.members[j % this.members.length];
        }

        this.winners = await this.restService.getWinnersByPrize(this.prize.bid, this.prize.pid);
    }

    async onPrizeChange() {
        this.winners = await this.restService.getWinnersByPrize(this.prize.bid, this.prize.pid);
    }


    onRandomClick() {
        console.log("后台直接随机生成...");

        this.restService.getRandomWinner(this.prize.bid, this.prize.pid);
	}


    onStopClick() {
        this.rolling = false;
    }


	onRollClick() {
		console.log("开始抽奖一次...");

        this.rolling = true;
        this.ready = false;

		this.rotation = 0;

        this.roll();
	}


	// 执行循环...
    async roll() {

        console.log("执行抽奖过程...");

        this.members = await this.restService.getMembersByBingo(this.bingo.id);
        this.winner = await this.restService.getRandomWinner(this.bingo.id, this.prize.pid);

        // 由于滚筒里面是显示三个，
		// 所以如果抽中最后一个，还要补一个
		// 滚筒总长度则是所有人员*2 + 1
        this.memberItems = new Array(this.members.length * 2 + 1);

        let i = -1;
        for (let j = 0; j < this.memberItems.length; j ++) {
        	if (i == -1) {
                if (this.members[j % this.members.length].id == this.winner.member.id) {
                	i = j;
				}
			}
            this.memberItems[j] = this.members[j % this.members.length];
        }

        console.log(this.winner);


        // 计算总共需要滚动的高度
        this.rotationRepeat = 0 - this.members.length * this.rotationHeight
        this.rotationOffset = this.rotationHeight - (this.members.length + i) * this.rotationHeight;

        console.log(this.rotationOffset);

        this.rotationSpeed = 30;

        this.updateRotation();
	}

    updateRotation() {
        //console.log(this.rotation);
        // 无限循环
        if (this.rolling) {
            if (this.rotation <= this.rotationRepeat) {
                this.rotation = 0;
            }
        }
        else {
            if (this.rotation < this.rotationOffset) {

                this.onPrizeChange();
                this.ready = true;

                return;
            }
            else {
                //console.log(this.rotation)
                if ((! this.rolling) && this.rotationSpeed > 5) {
                    this.rotationSpeed = Math.floor((this.rotationOffset - this.rotation) / this.rotationOffset * 30.0);
                }
            }
        }



        this.rotation = this.rotation - this.rotationSpeed;
        if (Math.abs(this.rotation - this.rotationOffset) < 5) {
            this.rotation = this.rotationOffset;
        }

        setTimeout(() => { this.updateRotation() }, 5);
    }

	
}
